package com.xiezc.service;


import com.xiezc.dto.Spider;
import com.xiezc.entity.Story;
import com.xiezc.entity.StoryContent;
import com.xiezc.util.XException;
import com.xiezc.util.XUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * author: Administrator
 * createTime: 2017/9/15 0015 下午 23:44
 */
@Service
@Slf4j
public class XCrawler extends Crawler {


    /**
     * 开始方法
     *
     * @param url
     */
    public void start(String url) {
        crawler(url, "parse_top_dayvisit");
    }

    /**
     * 解析小说排行榜网页的方法
     *
     * @param doc
     */
    public void parse_top_dayvisit(Document doc, String currentUrl) {
        log.info("parse_top_dayvisit;  url:" + currentUrl);

        Elements elements = doc.select(".blue");
        elements.stream().forEach(element -> {
            String url = element.attr("href");
            crawler(url, currentUrl, "parse_story_info");
        });
        doc.select(".page").stream().forEach(element -> {
            element.getElementsByTag("a").stream().forEach(element1 -> {
                String next = element1.text().trim();
                if ("下页".equals(next) || "下一页".equals(next)) {
                    String url = element1.attr("href");
                    crawler(url, currentUrl, "parse_top_dayvisit");
                }
            });
        });
    }

    /**
     * 解析小说 简介页面
     *
     * @param currentUrl
     * @return
     */
    public Story parse_story_info(Document doc, String currentUrl) {
        log.info("parse_story_info;  url:" + currentUrl);
        Story story = new Story();
        story.setUrl(currentUrl);

        String intro_info = doc.select(".intro_info").get(0).text();
        story.setSummary(intro_info);

        Elements elements = doc.select(".block_txt2");
        elements.stream().forEach(element -> {
            String name = elements.select("h2").get(0).text();
            story.setName(name);
            Elements elements1 = element.select("p");
            elements1.stream().forEach(element1 -> {
                String content = element1.text();
                //获得分类
                if (content.contains("分类：")) {
                    story.setClass_(content.replaceAll("分类：", ""));
                    return;
                }
                if (content.contains("作者：")) {
                    story.setAuthor(content.replaceAll("作者：", ""));
                    return;
                }
                if (content.contains("状态：")) {
                    story.setStatus(content.replaceAll("状态：", ""));
                    return;
                }
                if (content.contains("更新：")) {
                    story.setUpdateTime(content.replaceAll("更新：", ""));
                    return;
                }
                if (content.contains("最新：")) {
                    story.setLatestChapter(content.replaceAll("最新：", ""));
                    String url = element1.attr("href");
                    story.setLatestChapterUrl(XUtil.dealUrl(url, currentUrl));
                    return;
                }
            });
        });

        doc.select(".chapter").get(0).getElementsByTag("a").stream().forEach(element -> {
            String url = element.attr("href");
            this.crawler(url, currentUrl, "parse_wapbook");
        });
        story.setCreateTime(new Date());
        return story;
    }


    public StoryContent parse_wapbook(Document doc, String currentUrl) {
        log.info("parse_wapbook;  url:" + currentUrl);
        StoryContent storyContent = new StoryContent();
        storyContent.setUrl(currentUrl);

        String text = doc.select("div.text").get(0).text().trim();
        storyContent.setContent(text);
        String title = doc.select(".title").get(0).text().trim();
        storyContent.setTitle(title);
        storyContent.setUrl(currentUrl);
        storyContent.setCreateTime(new Date());

        doc.select(".navigator-nobutton").get(0).select("a").stream().forEach((Element element) -> {
            String content = element.text();
            if ("上一页".equals(content)) {
                String url = element.attr("href");
                this.crawler(url, currentUrl, "parse_wapbook");
            }
        });
        return storyContent;
    }

}
